package com.gl.rent;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.gl.common.util.SecurityUtils;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.ApartmentLabel;
import com.haixiaoke.saas.repository.service.IApartmentLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 公寓标签Controller
 *
 * @author qixi
 * @date 2023-05-11
 */
@RestController
@RequestMapping("/apartment/label")
public class ApartmentLabelController extends BaseController {

    @Autowired
    private IApartmentLabelService apartmentLabelService;


    /**
     * 查询公寓标签列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ApartmentLabel apartmentLabel) {
        startPage();
        List<ApartmentLabel> list = apartmentLabelService.selectApartmentLabelList(apartmentLabel);
        return getDataTable(list);
    }


    /**
     * 获取公寓标签详细信息
     */
    @GetMapping(value = "/{labelId}")
    public AjaxResult getInfo(@PathVariable("labelId") Long labelId) {
        return success(apartmentLabelService.selectApartmentLabelByLabelId(labelId));
    }

    /**
     * 新增公寓标签
     */
    @PostMapping
    public AjaxResult add(@RequestBody ApartmentLabel apartmentLabel) {
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        apartmentLabel.setCreateBy(id);
        apartmentLabel.setOrgId(0L);

        if (!apartmentLabelService.checkOrgIdLabelNameUnique(apartmentLabel.getOrgId(), apartmentLabel.getLabelName())) {
            return error("标签已存在");
        }
        return toAjax(apartmentLabelService.insertApartmentLabel(apartmentLabel));
    }

    /**
     * 修改公寓标签
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ApartmentLabel apartmentLabel) {
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        apartmentLabel.setUpdateBy(id);

        ApartmentLabel label = apartmentLabelService.selectApartmentLabelByLabelId(apartmentLabel.getLabelId());
        if (!ObjectUtil.equals(label.getLabelName(), apartmentLabel.getLabelName())) {
            if (!apartmentLabelService.checkOrgIdLabelNameUnique(apartmentLabel.getOrgId(), apartmentLabel.getLabelName())) {
                return error("标签已存在");
            }
        }

        return toAjax(apartmentLabelService.updateApartmentLabel(apartmentLabel));
    }

    /**
     * 删除公寓标签
     */
    @DeleteMapping("/{labelIds}")
    public AjaxResult remove(@PathVariable Long[] labelIds) {
        return toAjax(apartmentLabelService.deleteApartmentLabelByLabelIds(labelIds));
    }
}
